//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.滑动窗口.定长滑动窗口;

import java.util.Arrays;

public class demo_1984 {
    public demo_1984() {
    }

    public static void main(String[] args) {
        int[] nums = new int[]{9, 4, 1, 7};
        System.out.println(minimumDifference(nums, 2));
    }

    public static int minimumDifference(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;
        int out = Integer.MAX_VALUE;
        int i = 0;

        for(int j = k - 1; i <= n - k; ++j) {
            out = Math.min(out, nums[j] - nums[i]);
            ++i;
        }

        return out;
    }
}
